package com.xxl.job.core.handler.impl;

import com.autonavi.yunda.yunji.common.utils.SpringUtil;
import com.autonavi.yunda.yunji.core.engine.Engine;
import com.autonavi.yunda.yunji.core.engine.context.EngineContext;
import com.autonavi.yunda.yunji.core.engine.script.InterfaceScriptClass;
import com.autonavi.yunda.yunji.core.enums.EngineType;
import com.autonavi.yunda.yunji.core.vo.HttpRequest;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.IJobHandler;

/**
 * @author xuxueli 2019-12-11 21:12:18
 */
public class EngineJobHandler extends IJobHandler {
    public final InterfaceScriptClass scriptClass;

    public EngineJobHandler(InterfaceScriptClass scriptClass) {
        this.scriptClass = scriptClass;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof EngineJobHandler)) {
            return false;
        }
        EngineJobHandler other = (EngineJobHandler) obj;
        return this.scriptClass.equals(other.scriptClass);
    }


    @Override
    public void execute() {
        EngineContext engineContext = new EngineContext(scriptClass);
        engineContext.engineType = EngineType.SERIAL;
        String jobParam = XxlJobHelper.getJobParam();
        HttpRequest request = HttpRequest.fromJopParam(jobParam);
        Engine engine = SpringUtil.getBean(Engine.class);
        engine.execEngineWorker(engineContext, request);
    }

    @Override
    public String toString() {
        return super.toString() + "[" + scriptClass.interfaceId + "]";
    }
}
